package org.dei.dataserver;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Statement;
import java.util.Arrays;

public class BlobDumper {

	private static Connection conn;
	
	private static void checkAndWrite(InputStream is, PrintStream fos){
		byte b1, b0;
		Integer readData;
		try {
			b0 = (byte) is.read();
			b1 = (byte) is.read();
			if(b1 >= 0)
				readData = (((int)b1 << 8) & 0x0000ff00) |((int)b0&0x000000ff) ;
			else
				readData = 0xffff0000 | (((int)b1 << 8) & 0x0000ff00) |((int)b0&0x000000ff) ;
			if(readData != -1)
				fos.println(readData);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void initializeDB() {
		
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost/perla?" + "user=perla&password=perla"); //sax12fr");
			
			// Inizializzo le varie query...
		
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long eventcount = 0L;
		File fx, fy, fz;
		PrintStream fosx, fosy, fosz;
		boolean enable_slash_n = false;
		boolean enable_x, enable_y, enable_z;
		fosx = fosy = fosz = null;
		enable_x = enable_y = enable_z = false;
		
		int argc = args.length;
		
		if(argc == 0)
			System.exit(0);
		
		if(argc == 1){
			enable_x = enable_y = enable_z = true;
		}
		
		if(argc > 1){
			String _m_args[] = Arrays.copyOfRange(args, 1, args.length -1);
			for(String s: _m_args){
				if(s.equalsIgnoreCase("x"))
					enable_x = true;
				if(s.equalsIgnoreCase("y"))
					enable_y = true;
				if(s.equalsIgnoreCase("z"))
					enable_z = true;
				if(s.equalsIgnoreCase("csv"))
					enable_slash_n = true;
			}
		}
		
		long timestamp;
		BlobDumper.initializeDB();
		try {
			Statement stm = conn.createStatement();
			if(stm.execute("SELECT DATA, TIMESTAMP FROM bibi")){
				ResultSet res = stm.getResultSet();
				while (res.next()) {
					
					timestamp = res.getLong("TIMESTAMP");
					Blob blob = res.getBlob("DATA");			
		
					if(enable_x) {
						fx = new File(args[0] + "_" + eventcount + "_x");
						fosx = new PrintStream(fx);
					}
					if(enable_y) {
						fy = new File(args[0] + "_" + eventcount + "_y");
						fosy = new PrintStream(fy);
					}
					if(enable_z) {
						fz = new File(args[0] + "_" + eventcount + "_z");
						fosz = new PrintStream(fz);
					}
					eventcount++;
					InputStream is = blob.getBinaryStream(); 
					
					while (is.available() > 0) {
						
						if (enable_x) {
							checkAndWrite(is, fosx);
							if(enable_slash_n)
								fosx.print('\n');
						}
						if (enable_y) {
							checkAndWrite(is, fosy);
							if(enable_slash_n)
								fosx.print('\n');
						}
						if (enable_z) {
							checkAndWrite(is, fosz);
							if(enable_slash_n)
								fosx.print('\n');						}
					}
				}
			}
			else{
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// TODO Auto-generated method stub

	}

}
